//
//  CaseManagerConrentController.m
//  QuickCure1
//
//  Created by zhouxuanhe on 16/11/1.
//  Copyright © 2016年 maoge. All rights reserved.
//
//#define isPop (_txf_name.text == nil)||(_txf_idNum.text == nil)||(_txf_phone.text == nil)||(_TV_content.text == nil)||([_cellNO2.detailTextLabel.text isEqualToString:@"请选择:"])||([_cellNO1.detailTextLabel.text isEqualToString:@"请选择:"])||(_time == nil)||(_img_pic == nil)
#import "CaseManagerContentController.h"
#import "CaseManagerTypeController.h"
#import "CaseManagerDetailController.h"
#import "SVProgressHUD.h"

@interface CaseManagerContentController ()<UITextViewDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate>
//姓名,ID,电话
@property (weak, nonatomic) IBOutlet UITextField *txf_name;
@property (weak, nonatomic) IBOutlet UITextField *txf_idNum;
@property (weak, nonatomic) IBOutlet UITextField *txf_phone;
//textView中的内容
@property (weak, nonatomic) IBOutlet UITextView *TV_content;
//添加图片按钮
@property (weak, nonatomic) IBOutlet UIButton *btn_picture;

//标签按钮
@property (weak, nonatomic) IBOutlet UIButton *btn_add;
//提示文字
@property (weak, nonatomic) IBOutlet UILabel *lbl_placeHolder;
//两个cell
@property (weak, nonatomic) IBOutlet UITableViewCell *cellNO1;
@property (weak, nonatomic) IBOutlet UITableViewCell *cellNO2;

//用来传递照片
@property (nonatomic,strong) UIImage *img_pic;
//be used to save data accord dic
@property (nonatomic,strong) NSDictionary *dicList;
//the current  time
@property (nonatomic,copy) NSString *time;


//接收block的numType的值
@property (nonatomic,assign)NSNumber *numType;

@end
static NSString *cellContent = @"cellContent";
@implementation CaseManagerContentController{
    //添加ImagePickerController成员变量
    UIImagePickerController *_imagePicker;
}
- (IBAction)buttonClickAddPicture:(UIButton *)sender {
    //访问相册,跳转到相册的控制器(需要info.plist权限)
    [self presentViewController:_imagePicker animated:YES completion:^{
        //NSLog(@"222222222");
    }];
}
#pragma mark -
#pragma mark - 相册代理方法,自动调用
//这个方法是协议UIImagePickerControllerDelegate里面的，选择图片结束的时候就会自动调用
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(nullable NSDictionary *)editingInfo{
    
    //设置头像
    [_btn_picture setImage:image forState:UIControlStateNormal];
    _img_pic = image;
    //将系统相册消失掉
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -
#pragma mark - 视图加载
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //create the UIBarButtonItem , the name is itemDone
    UIBarButtonItem *itemDone = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(popToCaseManager)];
    //add to the  rightBarButtonItem of navigationItem
    self.navigationItem.rightBarButtonItem = itemDone;
    
    //修改下一级控制器的返回按钮
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] init];
    backButtonItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backButtonItem;
    //调用初始化方法
    [self setupUI];
}

#pragma mark -
#pragma mark - popToCaseManagerVC / send block maseger
-(void)popToCaseManager
{
    //warning send block
    //Refresh the data
    [self.tableView reloadData];
    //获取当前时间
    _time = [self getCurrentTime];
    
    NSLog(@"%@",_dicList);
    //判断是否可以传值(字典)
    BOOL isPop = (_txf_name.text == nil)||(_txf_idNum.text == nil)||(_txf_phone.text == nil)||(_TV_content.text == nil)||([_cellNO2.detailTextLabel.text isEqualToString:@"请选择:"])||([_cellNO1.detailTextLabel.text isEqualToString:@"请选择:"])||(_time == nil)||(_img_pic == nil);
    
    //字典中的数据不能为空,如果为空则不能反回,进行提示
    if (isPop) {
        [SVProgressHUD showErrorWithStatus:@"内容不能为空,请重新输入"];
    }else{
        //将数据保存到字典
        _dicList = @{@"lbl_name":_txf_name.text,@"lbl_idCard":_txf_idNum.text,@"lbl_phone":_txf_phone.text,@"lbl_content":_TV_content.text,@"lbl_diseaseDetail": _cellNO2.detailTextLabel.text,@"lbl_diseaseType":_cellNO1.detailTextLabel.text,@"lbl_time":_time,@"img_pic":_img_pic};
        
        //send dic
        if (_BlockContent) {
            _BlockContent(_dicList);
        }
        //pop to previous VC
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark -
#pragma mark - add disease content
- (IBAction)buttonClickAdd:(UIButton *)sender {
    
    //the buttonAction not be used
}

#pragma mark -
#pragma mark - Initialize the interface
-(void)setupUI
{
    /****************************************************/
    //键盘类型
    _txf_idNum.keyboardType = UIKeyboardTypeNumberPad;
    _txf_phone.keyboardType = UIKeyboardTypeNumberPad;
    
    /** 访问相册相关代码 **/
    _imagePicker=[[UIImagePickerController alloc] init];
    
    _imagePicker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    //设置代理，到@interface后面遵守协议
    _imagePicker.delegate = self;
    _imagePicker.allowsEditing = YES;
    /************************************/
    
    //设置代理
    self.TV_content.delegate = self;
    //刷新数据
    [self.tableView reloadData];
    
}

#pragma mark -
#pragma mark - TextView delegate - be used when changed the textView
- (void)textViewDidChange:(UITextView *)textView
{
    //隐藏提示文字
    self.lbl_placeHolder.hidden = YES;
    
    //取消按钮点击权限，并显示提示文字
    if (textView.text.length == 0) {
        self.lbl_placeHolder.hidden = NO;
    }
    
}


#pragma mark -
#pragma mark - According to segue to find nextVC,receive the block
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //通过segue找到目标控制器,以便接收block的传值
    CaseManagerTypeController *typeVC = segue.destinationViewController;
    typeVC.BlockCaseType = ^(NSString *cellStringType){
        _cellNO1.detailTextLabel.text = cellStringType;
    };
    typeVC.BlockCaseTypeNum = ^(NSNumber *num){
        _numType = num;
    };//接收nsnumber形式的传值
}



#pragma mark -
#pragma mark - click cell return to someVC ,and receive block
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    /****************************************************/
    //根据indexPath获取cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //取消选中状态
    [cell setSelected:NO];
    [cell setHighlighted:NO];
    /****************************************************/
    
    //进行判断如果没有传值就不跳转
    if (_numType == nil) {
        return;
    }else{
        CaseManagerDetailController *detailVC = [[CaseManagerDetailController alloc] init];
        if (indexPath.section == 0 && indexPath.row == 2) {
            detailVC.numType = _numType;
            [self.navigationController pushViewController:detailVC animated:YES];
            detailVC.BlockCaseDetail = ^(NSString *cellStringDetail){
                _cellNO2.detailTextLabel.text = cellStringDetail;
                
            };
        }
    }
    
}

#pragma mark -
#pragma mark - get current time
/**
 获取当前时间
 */
-(NSString *)getCurrentTime
{
    //时间格式
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter = [[NSDateFormatter alloc] init];
    //2016-11-2 16:23:27
    [dateformatter setDateFormat:@"YYYY-MM-dd HH-mm-ss"];
    
    NSString * timeString=[dateformatter stringFromDate:senddate];
    
    //  NSLog(@"locationString:%@",timeString);
    return timeString;
    
}



















@end
